#ifndef STATE_PTR_H
#define STATE_PTR_H

#include <ompl/base/SpaceInformation.h>
#include <ompl/base/State.h>

// Manage memory of a State pointer with raii pattern.
class StatePtr {
	ompl::base::SpaceInformationPtr si_;
	ompl::base::State * state_;

public:
	StatePtr() : state_(0) {}

	StatePtr(const ompl::base::SpaceInformationPtr &si,
		const ompl::base::State * otherState)
		: si_(si), state_(si->cloneState(otherState)) {
	}

	explicit StatePtr(const ompl::base::SpaceInformationPtr &si)
		: si_(si), state_(si->allocState()) {
	}

	StatePtr(const StatePtr & other)
		: si_(other.si_), state_(other.si_->cloneState(other.state_)) {
	}

	StatePtr &operator=(const StatePtr & other) {
		si_ = other.si_;
		state_ = si_->cloneState(other.state_);
		return *this;
	}

	virtual ~StatePtr() {
		si_->freeState(state_);
	}

	bool operator==(const StatePtr & other) const {
		return si_->equalStates(state_, other.state_);
	}

	ompl::base::SpaceInformationPtr getSpaceInformation() const {
		return si_;
	}

	operator const ompl::base::State * () const {
		return const_cast<const ompl::base::State *>(state_);
	}

	operator ompl::base::State * () const {
		return state_;
	}
};

#endif
